﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace Lesson3Practice
{
	class Program
	{
		static void Main(string[] args)
		{
			const String fileName = "Orders.xml";
			
			CreateResultsFile(fileName);

			Console.ReadLine();
		}

		#region practice

		private static void CreateResultsFile(String feedFileName)
		{
			var doc = XDocument.Load(feedFileName);

			var result = new XElement("ModifiedOrders",
									  from o in doc.Descendants("Order")
									  select new XElement("Order",
														  new XAttribute("OrderID", (String)o.Attribute("OrderNumber")),
														  new XAttribute("TotalFreight",
																		 o.Elements("LineItem").Sum(
																			li => (Decimal)li.Attribute("Freight"))),
														  from li in o.Elements("LineItem")
														  select new XElement("Item",
																			  new XAttribute("Number", (Int32)li.Attribute("Line")),
																			  new XAttribute("PID", (Int32)li.Attribute("PID")),
																			  new XAttribute("Qty", (Int32)li.Attribute("Qty")),
																			  new XAttribute("Price", (Decimal)li.Attribute("Price")),
																			  new XAttribute("Freight", (Decimal)li.Attribute("Freight"))
																			  )
														)
									);


			result.Save(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Results.xml"));
		}

		#endregion
	}
}
